home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / poplib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  8.6 KB  |  324 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5. import socket
  6. __all__ = [
  7.     'POP3',
  8.     'error_proto',
  9.     'POP3_SSL']
  10.  
  11. class error_proto(Exception):
  12.     pass
  13.  
  14. POP3_PORT = 110
  15. POP3_SSL_PORT = 995
  16. CR = '\r'
  17. LF = '\n'
  18. CRLF = CR + LF
  19.  
  20. class POP3:
  21.     
  22.     def __init__(self, host, port = POP3_PORT):
  23.         self.host = host
  24.         self.port = port
  25.         msg = 'getaddrinfo returns an empty list'
  26.         self.sock = None
  27.         for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
  28.             (af, socktype, proto, canonname, sa) = res
  29.             
  30.             try:
  31.                 self.sock = socket.socket(af, socktype, proto)
  32.                 self.sock.connect(sa)
  33.             except socket.error:
  34.                 msg = None
  35.                 if self.sock:
  36.                     self.sock.close()
  37.                 
  38.                 self.sock = None
  39.                 continue
  40.  
  41.         
  42.         if not self.sock:
  43.             raise socket.error, msg
  44.         
  45.         self.file = self.sock.makefile('rb')
  46.         self._debugging = 0
  47.         self.welcome = self._getresp()
  48.  
  49.     
  50.     def _putline(self, line):
  51.         if self._debugging > 1:
  52.             print '*put*', repr(line)
  53.         
  54.         self.sock.sendall('%s%s' % (line, CRLF))
  55.  
  56.     
  57.     def _putcmd(self, line):
  58.         if self._debugging:
  59.             print '*cmd*', repr(line)
  60.         
  61.         self._putline(line)
  62.  
  63.     
  64.     def _getline(self):
  65.         line = self.file.readline()
  66.         if self._debugging > 1:
  67.             print '*get*', repr(line)
  68.         
  69.         if not line:
  70.             raise error_proto('-ERR EOF')
  71.         
  72.         octets = len(line)
  73.         if line[-2:] == CRLF:
  74.             return (line[:-2], octets)
  75.         
  76.         if line[0] == CR:
  77.             return (line[1:-1], octets)
  78.         
  79.         return (line[:-1], octets)
  80.  
  81.     
  82.     def _getresp(self):
  83.         (resp, o) = self._getline()
  84.         if self._debugging > 1:
  85.             print '*resp*', repr(resp)
  86.         
  87.         c = resp[:1]
  88.         if c != '+':
  89.             raise error_proto(resp)
  90.         
  91.         return resp
  92.  
  93.     
  94.     def _getlongresp(self):
  95.         resp = self._getresp()
  96.         list = []
  97.         octets = 0
  98.         (line, o) = self._getline()
  99.         while line != '.':
  100.             if line[:2] == '..':
  101.                 o = o - 1
  102.                 line = line[1:]
  103.             
  104.             octets = octets + o
  105.             list.append(line)
  106.             (line, o) = self._getline()
  107.         return (resp, list, octets)
  108.  
  109.     
  110.     def _shortcmd(self, line):
  111.         self._putcmd(line)
  112.         return self._getresp()
  113.  
  114.     
  115.     def _longcmd(self, line):
  116.         self._putcmd(line)
  117.         return self._getlongresp()
  118.  
  119.     
  120.     def getwelcome(self):
  121.         return self.welcome
  122.  
  123.     
  124.     def set_debuglevel(self, level):
  125.         self._debugging = level
  126.  
  127.     
  128.     def user(self, user):
  129.         return self._shortcmd('USER %s' % user)
  130.  
  131.     
  132.     def pass_(self, pswd):
  133.         return self._shortcmd('PASS %s' % pswd)
  134.  
  135.     
  136.     def stat(self):
  137.         retval = self._shortcmd('STAT')
  138.         rets = retval.split()
  139.         if self._debugging:
  140.             print '*stat*', repr(rets)
  141.         
  142.         numMessages = int(rets[1])
  143.         sizeMessages = int(rets[2])
  144.         return (numMessages, sizeMessages)
  145.  
  146.     
  147.     def list(self, which = None):
  148.         if which is not None:
  149.             return self._shortcmd('LIST %s' % which)
  150.         
  151.         return self._longcmd('LIST')
  152.  
  153.     
  154.     def retr(self, which):
  155.         return self._longcmd('RETR %s' % which)
  156.  
  157.     
  158.     def dele(self, which):
  159.         return self._shortcmd('DELE %s' % which)
  160.  
  161.     
  162.     def noop(self):
  163.         return self._shortcmd('NOOP')
  164.  
  165.     
  166.     def rset(self):
  167.         return self._shortcmd('RSET')
  168.  
  169.     
  170.     def quit(self):
  171.         
  172.         try:
  173.             resp = self._shortcmd('QUIT')
  174.         except error_proto:
  175.             val = None
  176.             resp = val
  177.  
  178.         self.file.close()
  179.         self.sock.close()
  180.         del self.file
  181.         del self.sock
  182.         return resp
  183.  
  184.     
  185.     def rpop(self, user):
  186.         return self._shortcmd('RPOP %s' % user)
  187.  
  188.     timestamp = re.compile('\\+OK.*(<[^>]+>)')
  189.     
  190.     def apop(self, user, secret):
  191.         m = self.timestamp.match(self.welcome)
  192.         if not m:
  193.             raise error_proto('-ERR APOP not supported by server')
  194.         
  195.         import hashlib
  196.         digest = hashlib.md5(m.group(1) + secret).digest()
  197.         digest = ''.join(map((lambda x: '%02x' % ord(x)), digest))
  198.         return self._shortcmd('APOP %s %s' % (user, digest))
  199.  
  200.     
  201.     def top(self, which, howmuch):
  202.         return self._longcmd('TOP %s %s' % (which, howmuch))
  203.  
  204.     
  205.     def uidl(self, which = None):
  206.         if which is not None:
  207.             return self._shortcmd('UIDL %s' % which)
  208.         
  209.         return self._longcmd('UIDL')
  210.  
  211.  
  212.  
  213. class POP3_SSL(POP3):
  214.     
  215.     def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None):
  216.         self.host = host
  217.         self.port = port
  218.         self.keyfile = keyfile
  219.         self.certfile = certfile
  220.         self.buffer = ''
  221.         msg = 'getaddrinfo returns an empty list'
  222.         self.sock = None
  223.         for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
  224.             (af, socktype, proto, canonname, sa) = res
  225.             
  226.             try:
  227.                 self.sock = socket.socket(af, socktype, proto)
  228.                 self.sock.connect(sa)
  229.             except socket.error:
  230.                 msg = None
  231.                 if self.sock:
  232.                     self.sock.close()
  233.                 
  234.                 self.sock = None
  235.                 continue
  236.  
  237.         
  238.         if not self.sock:
  239.             raise socket.error, msg
  240.         
  241.         self.file = self.sock.makefile('rb')
  242.         self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile)
  243.         self._debugging = 0
  244.         self.welcome = self._getresp()
  245.  
  246.     
  247.     def _fillBuffer(self):
  248.         localbuf = self.sslobj.read()
  249.         if len(localbuf) == 0:
  250.             raise error_proto('-ERR EOF')
  251.         
  252.         self.buffer += localbuf
  253.  
  254.     
  255.     def _getline(self):
  256.         line = ''
  257.         renewline = re.compile('.*?\\n')
  258.         match = renewline.match(self.buffer)
  259.         while not match:
  260.             self._fillBuffer()
  261.             match = renewline.match(self.buffer)
  262.         line = match.group(0)
  263.         self.buffer = renewline.sub('', self.buffer, 1)
  264.         if self._debugging > 1:
  265.             print '*get*', repr(line)
  266.         
  267.         octets = len(line)
  268.         if line[-2:] == CRLF:
  269.             return (line[:-2], octets)
  270.         
  271.         if line[0] == CR:
  272.             return (line[1:-1], octets)
  273.         
  274.         return (line[:-1], octets)
  275.  
  276.     
  277.     def _putline(self, line):
  278.         if self._debugging > 1:
  279.             print '*put*', repr(line)
  280.         
  281.         line += CRLF
  282.         bytes = len(line)
  283.         while bytes > 0:
  284.             sent = self.sslobj.write(line)
  285.             if sent == bytes:
  286.                 break
  287.             
  288.             line = line[sent:]
  289.             bytes = bytes - sent
  290.  
  291.     
  292.     def quit(self):
  293.         
  294.         try:
  295.             resp = self._shortcmd('QUIT')
  296.         except error_proto:
  297.             val = None
  298.             resp = val
  299.  
  300.         self.sock.close()
  301.         del self.sslobj
  302.         del self.sock
  303.         return resp
  304.  
  305.  
  306. if __name__ == '__main__':
  307.     import sys
  308.     a = POP3(sys.argv[1])
  309.     print a.getwelcome()
  310.     a.user(sys.argv[2])
  311.     a.pass_(sys.argv[3])
  312.     a.list()
  313.     (numMsgs, totalSize) = a.stat()
  314.     for i in range(1, numMsgs + 1):
  315.         (header, msg, octets) = a.retr(i)
  316.         print 'Message %d:' % i
  317.         for line in msg:
  318.             print '   ' + line
  319.         
  320.         print '-----------------------'
  321.     
  322.     a.quit()
  323.  
  324.